﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Xml.Serialization;
using Ppt = Microsoft.Office.Interop.PowerPoint;
using ObjectModel.PowerPoint;
using DataFactory;

namespace ObjectModel.PowerPoint.New
{
    public class LinkedSlideManager
    {
        const string XML_ROOT_NAME = "LinkedSlides";
        readonly PPTSerializer mSerializer = new PPTSerializer();

        public LinkedSlideDictionary Slides { get; private set; }

        public LinkedSlideManager(Ppt.Presentation pres)
        {
            Slides = PPTSerializer.Deserialize<LinkedSlideDictionary>(pres, XML_ROOT_NAME) ??
                new LinkedSlideDictionary();

            Globals.PresentationEvents[pres].PresentationBeforeSave += OnPresentationBeforeSave;
        }

        void OnPresentationBeforeSave(object sender, PresentationBeforeSaveEventArgs e)
        {
            if (Slides.Count > 0)
                mSerializer.SerializeToPresentation(e.Presentation, Slides, XML_ROOT_NAME);
        }
    }
}
